package neuralNetwork;

import neuralNetwork.NeuralNetwork.ArrayOperations;

public class CTRNNLayer {
	private CTRNNNeuron[] neurons; 

	public CTRNNLayer(CTRNNLayer nextLayer, int nofNeurons, boolean isInputLayer) {
		this.neurons = new CTRNNNeuron[nofNeurons]; 
		for (int i = 0; i < nofNeurons; i++) {
			if (isInputLayer) {
				neurons[i] = new CTRNNNeuron(null, nextLayer); 
			} else {
				neurons[i] = new CTRNNNeuron(this, nextLayer); 
			}
		}
	}

	public int nofWeights(){
		int nofWeights = 0; 
		for (CTRNNNeuron neuron : neurons) {
			nofWeights += neuron.nofWeights(); 
		}
		return nofWeights; 
	}

	public double[] getWeights() {
		double[] weights = new double[nofWeights()];
		int index = 0; 
		for (CTRNNNeuron neuron : neurons) {
			index = ArrayOperations.insertData(weights, neuron.getWeights(), index);
		}
		return weights;
	}

	public void setWeights(double[][] neuronWeightList) {
		for (int i = 0; i < neuronWeightList.length; i ++){
			setNeuronWeights(neurons[i], neuronWeightList[i]); 
		}
	}
	
	private void setNeuronWeights(CTRNNNeuron neuron, double[] parameters){
		double[] weights = new double[parameters.length-3]; 
		for (int i = 0; i < weights.length; i ++){
			weights[i] = parameters[i+3]; 
		}
		neuron.setWeights(parameters[0], parameters[1], parameters[2], weights); 
	}

	public CTRNNNeuron get(int index){
		return neurons[index]; 
	}
	
	public int nofNeurons(){
		return neurons.length; 
	}
	
	public void fireUpdate(){
		for (CTRNNNeuron neuron : neurons) {
			neuron.fireLayerUpdate(); 
		}
		for (CTRNNNeuron neuron : neurons) {
			neuron.fireUpdate(); 
		}
	}
	
	public void setInput(boolean[] inputs){
		for (int i = 0; i < neurons.length; i ++) {
			neurons[i].setInputSensor(inputs[i]); 
		}
	}
	
	public double[] returnOutput(){
		double[] outputs = new double[neurons.length]; 
		for (int i = 0; i < neurons.length; i++) {
			outputs[i] = neurons[i].returnOutput(); 
		}
		return outputs;
	}
	
	public String toString(){
		String theString = "";
		for (int i = 0; i < neurons.length; i++) {
			CTRNNNeuron neuron = neurons[i];
			theString += neuron.toString() + ",   "; 
			
		}
		return theString; 
	}

	public int getNeuronSize() {
		return neurons[0].nofWeights();
	}
	
//	public void resetActivation(){
//		for(CTRNNNeuron neuron : neurons){
//			neuron.resetActivation();
//		}
//	}

	public String activationString() {
		String theString = "";
		for (int i = 0; i < neurons.length; i++) {
			CTRNNNeuron neuron = neurons[i];
			theString += neuron.activationString() + ", "; 
			
		}
		return theString;
	}
}
